package kr.hkit.powerjava.chapter14.submit;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Panel;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MileToKilo extends JFrame implements ActionListener {

	private JPanel contentPane;

	private JPanel panel;
	private JLabel lbl01;
	private InsertPanel inputText;
	private JPanel panel_1;
	private JButton btnTransfet;
	private JPanel panel_2;
	private ResultPanel outputText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MileToKilo frame = new MileToKilo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MileToKilo() {
		setTitle("마일을 킬로미터로 변환");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 325, 143);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 1, 0, 0));
		
		panel = new JPanel();
		contentPane.add(panel);
		
		
		inputText = new InsertPanel("거리를 마일 단위로 입력하시오");
		panel.add(inputText);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);
		
		btnTransfet = new JButton("변환");
		btnTransfet.addActionListener(this);
		panel_1.add(btnTransfet);
		
		panel_2 = new JPanel();
		contentPane.add(panel_2);
		
		outputText = new ResultPanel();
		panel_2.add(outputText);
		
	}

	public void actionPerformed(ActionEvent e) {
		int kilo = 0;
		if (e.getActionCommand().equals("변환")) {
			try{
				kilo = Integer.parseInt(inputText.getInsertText().trim());
				outputText.setResultText(kilo + " 마일은 " +kilo*1.6+" 킬로미터입니다.");
			} catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능합니다.");
                inputText.setInsertText("");
			}
		}
	}
}
